import Taro, { Component, Config } from '@tarojs/taro'
import Index from './pages/index/index'

import './app.scss'
import {base_url} from "./network/config";

import { STORAGE_KEY } from './utils/const'
import DataManager from "./utils/DataManager";
import singleConvenienceList from "./pages/convenience/singleConvenienceList";

const upDateManager = Taro.getUpdateManager();

class App extends Component {

  /**
   * 指定config的类型声明为: Taro.Config
   *
   * 由于 typescript 对于 object 类型推导只能推出 Key 的基本类型
   * 对于像 navigationBarTextStyle: 'black' 这样的推导出的类型是 string
   * 提示和声明 navigationBarTextStyle: 'black' | 'white' 类型冲突, 需要显示声明类型
   */
  config: Config = {
    pages: [
      'pages/index/index',
      'pages/car/carList',
      'pages/job/jobList',
      'pages/shop/shopList',
      'pages/me/me',
      'pages/job/jobDetail',
      'pages/car/carDetail',
      'pages/post/ridingPost',
      'pages/post/driverPost',
      'pages/shop/shopDetail',
      'pages/me/myCarList',
      'pages/me/myJobList',
      'pages/post/jobPost',
      'pages/post/shopInfoPost',
      'pages/me/shopRegister',
      'pages/me/myShopList',
      'pages/me/myCollections',
      'pages/me/bindMobile',
      'pages/protocol/ridingProtocol',
      'pages/protocol/driverProtocol',
      'pages/protocol/shopInfoProtocol',
      'pages/protocol/jobProtocol',
      'pages/protocol/promotionPage',
      'pages/common/commonWebPage',
      'pages/me/scoreExchangeList',
      'pages/me/myExchange',
      'pages/me/scorePost',
      'pages/shop/storeList',
      'pages/shop/storeDetail',
      'pages/me/addReceiveAddress',
      'pages/me/receiveAddressList',
      'pages/me/exchangeDetail',
      'pages/index/openAreaList',
      'pages/convenience/convenienceList',
      'pages/post/allPost',
      'pages/index/newsDetail',
      'pages/convenience/singleConvenienceList',
      'pages/me/myConvenienceList',

    ],
    window: {
      enablePullDownRefresh:true,
      backgroundTextStyle: 'dark',
      navigationBarBackgroundColor: '#fff',
      navigationBarTitleText: 'WeChat',
      navigationBarTextStyle: 'black',
    },
    tabBar: {
      custom:true,
      backgroundColor: '#fff',
      selectedColor: '#dc0032',
      list: [
        {
          pagePath: 'pages/index/index',
          text: '首页',
          iconPath: 'image/ic_tab_home.png',
          selectedIconPath: 'image/ic_tab_home_red.png',
        },
        {
          pagePath: 'pages/convenience/convenienceList',
          text: '便民信息',
          iconPath: 'image/ic_tab_car.png',
          selectedIconPath: 'image/ic_tab_car_red.png',
        },
        {
          pagePath: 'pages/job/jobList',
          text: '招聘',
          iconPath: 'image/ic_tab_job.png',
          selectedIconPath: 'image/ic_tab_job_red.png',
        },
        // {
        //   pagePath: 'pages/shop/shopList',
        //   text: '商家',
        //   iconPath: 'image/ic_tab_shop.png',
        //   selectedIconPath: 'image/ic_tab_shop_red.png',
        // },
        {
          pagePath: 'pages/me/me',
          text: '我的',
          iconPath: 'image/ic_tab_mine.png',
          selectedIconPath: 'image/ic_tab_mine_red.png',
        }
      ]
    },
    "usingComponents": {},
    permission: {
      "scope.userLocation": {
        "desc": "你的位置信息将用于定位效果展示"
      }
    }
  }

  componentDidMount () {
    // this.login()
    upDateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log(res.hasUpdate)
    })

    upDateManager.onUpdateReady(function () {
      Taro.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启小程序？',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            upDateManager.applyUpdate()
          }
        }
      })
    })
  }

  componentWillMount(){
    const params = this.$router.params;//获取所有参数

    const query = params.query ;//query里面是链接上带的参数

    if (query && query.scene){
      Taro.setStorageSync(STORAGE_KEY.SCENE , query.scene);
    }
  }

  login(){
    Taro.login({
      success(res){
        console.log('weixin login--' + JSON.stringify(res))
        let url = base_url + 'stateless/weixin/weixinAuth'
        Taro.request({
          url:url,
          data: {
            platform:'android_c',
            weixinId:DataManager.getWeiXinId(),
            loginChannel:'wechat',
            appId:'KC876D98B30UA40CJSNHI689FB33SDW',
            digest : 'dda484557c12febe48139d8f44051c13',
            code:res.code
          },
          method: 'GET',
          success(res1) {
            console.log('auth login--' + res1.data.data.openId)
            // Taro.getUserInfo({
            //   success(info){
                Taro.request({
                  url:base_url + 'login',
                  data:{
                    platform:'android_c',
                    weixinId:DataManager.getWeiXinId(),
                    loginChannel:'wechat',
                    appId:'KC876D98B30UA40CJSNHI689FB33SDW',
                    digest : 'dda484557c12febe48139d8f44051c13',
                    username:res1.data.data.openId,
                  },
                  header: { 'content-type': 'application/x-www-form-urlencoded' },
                  method: 'POST',
                  success(res2) {
                    console.log('res2=========]' + JSON.stringify(res2))
                    if (res2.data.data.token){
                      Taro.setStorageSync(STORAGE_KEY.TOKEN , res2.data.data.token);
                    }
                    Taro.setStorageSync(STORAGE_KEY.IS_BIND_MOBILE , res2.data.data.isBindMobile)
                    Taro.setStorageSync(STORAGE_KEY.IS_BIND_WECHAT , res2.data.data.isBindWechat)
                    Taro.setStorageSync(STORAGE_KEY.MOBILE , res2.data.data.mobile)
                    Taro.setStorageSync(STORAGE_KEY.IS_BUSINESS , res2.data.data.isBusiness)
                    Taro.setStorageSync(STORAGE_KEY.ADDRESS_NAME , res2.data.data.addressName)
                  },
                  fail(){

                  },
                })
              }
            })
          }
        })
    //   }
    // })
  }

  componentDidShow () {}

  componentDidHide () {}

  componentCatchError () {}

  render () {
    return (
      <Index />
    )
  }
}

Taro.render(<App />, document.getElementById('app'))
